package com.personal.portal.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.ContactMessage;
import com.personal.portal.admin.mapper.ContactMessageMapper;
import com.personal.portal.admin.service.ContactMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 联系消息服务实现类
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Service
public class ContactMessageServiceImpl implements ContactMessageService {

    @Resource
    private ContactMessageMapper contactMessageMapper;

    @Override
    public ContactMessage queryById(Long id) {
        validateId(id);
        return contactMessageMapper.selectOneById(id);
    }

    @Override
    public ContactMessage queryByEntity(ContactMessage contactMessage) {
        QueryWrapper queryWrapper = buildQueryWrapper(contactMessage);
        return contactMessageMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public List<ContactMessage> queryList(ContactMessage contactMessage) {
        QueryWrapper queryWrapper = buildQueryWrapper(contactMessage);
        return contactMessageMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public Result.PageResult<ContactMessage> queryPage(int pageNum, int pageSize, ContactMessage contactMessage) {
        QueryWrapper queryWrapper = buildQueryWrapper(contactMessage);
        Page<ContactMessage> paginate = contactMessageMapper.paginate(pageNum, pageSize, queryWrapper);
        return new Result.PageResult<>(paginate.getRecords(), paginate.getTotalRow(), pageNum, pageSize);
    }

    @Override
    public int insert(ContactMessage contactMessage) {
        validateContactMessage(contactMessage);
        return contactMessageMapper.insert(contactMessage);
    }

    @Override
    public int update(ContactMessage contactMessage) {
        if (contactMessage.getId() == null) {
            throw new IllegalArgumentException("联系消息ID不能为空");
        }
        validateId(contactMessage.getId());
        validateContactMessage(contactMessage);
        
        ContactMessage existing = contactMessageMapper.selectOneById(contactMessage.getId());
        if (existing == null) {
            throw new IllegalArgumentException("联系消息信息不存在");
        }
        
        return contactMessageMapper.update(contactMessage);
    }

    @Override
    public int deleteById(Long id) {
        validateId(id);
        
        ContactMessage existing = contactMessageMapper.selectOneById(id);
        if (existing == null) {
            throw new IllegalArgumentException("联系消息信息不存在");
        }
        
        return contactMessageMapper.deleteById(id);
    }

    @Override
    public List<Long> deleteBatch(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        List<Long> successIds = new ArrayList<>();
        List<Long> failedIds = new ArrayList<>();

        for (Long id : ids) {
            try {
                validateId(id);
                
                ContactMessage existing = contactMessageMapper.selectOneById(id);
                if (existing == null) {
                    failedIds.add(id);
                    continue;
                }

                int result = contactMessageMapper.deleteById(id);
                if (result > 0) {
                    successIds.add(id);
                } else {
                    failedIds.add(id);
                }
            } catch (Exception e) {
                failedIds.add(id);
            }
        }

        if (!successIds.isEmpty()) {
            return successIds;
        } else {
            throw new IllegalArgumentException("批量删除失败");
        }
    }

    /**
     * 验证联系消息信息
     */
    private void validateContactMessage(ContactMessage contactMessage) {
        if (contactMessage == null) {
            throw new IllegalArgumentException("联系消息信息不能为空");
        }
        if (StringUtils.isBlank(contactMessage.getSenderName())) {
            throw new IllegalArgumentException("发送者姓名不能为空");
        }
        if (StringUtils.isBlank(contactMessage.getSenderEmail())) {
            throw new IllegalArgumentException("发送者邮箱不能为空");
        }
        if (StringUtils.isBlank(contactMessage.getContent())) {
            throw new IllegalArgumentException("消息内容不能为空");
        }
    }

    /**
     * 验证ID
     */
    private void validateId(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("无效的联系消息ID");
        }
    }

    /**
     * 构建查询条件
     */
    private QueryWrapper buildQueryWrapper(ContactMessage contactMessage) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(ContactMessage::getId, contactMessage.getId(), contactMessage.getId() != null);
        queryWrapper.like(ContactMessage::getSenderName, contactMessage.getSenderName(), StringUtils.isNotEmpty(contactMessage.getSenderName()));
        queryWrapper.like(ContactMessage::getSenderEmail, contactMessage.getSenderEmail(), StringUtils.isNotEmpty(contactMessage.getSenderEmail()));
        queryWrapper.like(ContactMessage::getSubject, contactMessage.getSubject(), StringUtils.isNotEmpty(contactMessage.getSubject()));
        queryWrapper.eq(ContactMessage::getStatus, contactMessage.getStatus(), contactMessage.getStatus() != null);
        queryWrapper.eq(ContactMessage::getMessageType, contactMessage.getMessageType(), StringUtils.isNotEmpty(contactMessage.getMessageType()));

        queryWrapper.orderBy(ContactMessage::getSentTime, false);
        queryWrapper.orderBy(ContactMessage::getId, true);
        return queryWrapper;
    }
}